Defer type analysis of integer Literal when builtins.int is slow to resolve #18046
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This MR fixes two possible crashes related to
builtins.intnot being ready yet when aLiteralis analyzed. One happens ifbuiltins.intis entirely unavailable, and the other ifbuiltins.intis only available as a placeholder node.The first case is responsible for the crash if typeshed imports Literal from
typinginstead oftyping_extensions. Mypy hasLiteralavailable during the first pass ofbuiltins.pyi, which means it encountersLiteral[0]before it's seen theclass intstatement: python/typeshed#11247The second case occurs if
builtins.intis a subclass of something that's slow to resolve, which turns up if you try to makebuiltins.intinherit fromnumbers.Integral: python/typeshed#12894Both cases are resolved by deferring during
TypeAnalyser.analyze_literal_paramif the type of the literal isn't ready yet.After fixing those, an additional issue became apparent, where the r.h.s of a TypeAlias assignment is ready but
TypeAliasitself isn't ready yet, which caused mypy to say it wasn't a valid TypeAlias. An additional check and defer withinSemanticAnalyzer.visit_assignment_stmtfixes that. This didn't happen before becausetyping_extensions.Literalandtyping_extensions.TypeAliasresolved at the same speed, so it wasn't noticeable. Some problems shows up for deferred on any unresolved lhs type, so I restricted it to only defer for unresolved lhs types which were imported from typing or typing_extensions.